package com.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;


public class JwtUtil {

    private static String secretKey = "chenchaunzge1111iuafiuahfuahdishcaiscuisiucauihciuahuhiabi";

    //生成令牌
    public static String buildJwt(Map<String,Object> map){
        Claims claims = Jwts.claims(map);
        SecretKey key = Keys.hmacShaKeyFor(secretKey.getBytes());
        String compact = Jwts.builder()
                .addClaims(claims)
                .signWith(key)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7))
                .compact();
        return compact;

    }

    //解析令牌
    public static Claims parserJwt(String token){
        SecretKey key = Keys.hmacShaKeyFor(secretKey.getBytes());
        Claims body = Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(token)
                .getBody();
        return body;
    }
}
















// 获取并打印各个声明值
      /*  String s = body.toString();

        System.out.println(s);*/

        /*String name = body.get("name", String.class);
        String id = body.getId();
        String username = body.get("username", String.class);
        Date expiration = body.getExpiration();

        System.out.println("Name: " + name);
        System.out.println("ID: " + id);
        System.out.println("Username: " + username);
        System.out.println("Expiration: " + expiration);
        System.out.println("All Claims: " + body);*/
